import uuid

from flask import Blueprint, request, jsonify
from service.nw_project.nw_project_hsm_service import get_all_data, get_data_by_uuid, add_data, update_data, delete_data

# app = Flask(__name__)
# 创建 Blueprint
nw_project_hsm_blueprint = Blueprint('nw_project_hsm', __name__)


# 获取所有记录
@nw_project_hsm_blueprint.route('/api/data', methods=['GET'])
def get_data():
    try:
        data = get_all_data()
        return jsonify(data), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500


# 获取单条记录
@nw_project_hsm_blueprint.route('/api/data/<string:uuid>', methods=['GET'])
def get_single_data(uuid):
    try:
        data = get_data_by_uuid(uuid)
        if data:
            return jsonify(data), 200
        else:
            return jsonify({"message": "Data not found"}), 404
    except Exception as e:
        return jsonify({"error": str(e)}), 500


# 添加记录
@nw_project_hsm_blueprint.route('/api/data', methods=['POST'])
def add_new_data():
    try:
        data = request.get_json()
        dwbh = data.get('dwbh')
        project_uuid = data.get('project_uuid')
        kjnd = data.get('kjnd')
        hsm_xmbh = data.get('hsm_xmbh')
        hsm_xmnm = data.get('hsm_xmnm')
        hsm_name = data.get('hsm_name')
        hsm_fullname = data.get('hsm_fullname')
        hsm_xmlb = data.get('hsm_xmlb')
        calc = data.get('calc')
        qzq_type = data.get('qzq_type', 'KY')
        model = data.get('model')
        status = data.get('status', '1')  # 默认值是 '1'
        create_by = data.get('create_by', 'admin')
        remarks = data.get('remarks')

        add_data(uuid.uuid4(), dwbh, model, qzq_type, project_uuid, kjnd, hsm_xmbh, hsm_xmnm, hsm_name, hsm_fullname, hsm_xmlb, calc, status, create_by, remarks)
        return jsonify({"message": "Data added successfully!"}), 201
    except Exception as e:
        return jsonify({"error": str(e)}), 500


# 更新记录
@nw_project_hsm_blueprint.route('/api/data/<string:uuid>', methods=['PUT'])
def update_existing_data(uuid):
    try:
        data = request.get_json()
        dwbh = data.get('dwbh')
        project_uuid = data.get('project_uuid')
        kjnd = data.get('kjnd')
        hsm_xmbh = data.get('hsm_xmbh')
        hsm_xmnm = data.get('hsm_xmnm')
        hsm_name = data.get('hsm_name')
        hsm_fullname = data.get('hsm_fullname')
        hsm_xmlb = data.get('hsm_xmlb')
        calc = data.get('calc')
        qzq_type = data.get('qzq_type', 'KY')
        model = data.get('model')
        status = data.get('status', '1')  # 默认值是 '1'
        create_by = data.get('create_by', 'admin')
        remarks = data.get('remarks')

        update_data( uuid, dwbh, model, qzq_type, project_uuid, kjnd, hsm_xmbh, hsm_xmnm, hsm_name, hsm_fullname, hsm_xmlb, calc, status, create_by, remarks )
        return jsonify({"message": "Data updated successfully!"}), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500


# 删除记录
@nw_project_hsm_blueprint.route('/api/data/<string:uuid>', methods=['DELETE'])
def delete_existing_data(uuid):
    try:
        delete_data(uuid)
        return jsonify({"message": "Data deleted successfully!"}), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500


if __name__ == '__main__':
    # 在应用启动时创建表
    from service.nw_project.nw_project_hsm_service import create_table

    create_table()
    # app.run(debug=True)
